define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
  
	var Model = function(){
	    this.action  = '';
		this.callParent();
		this.renderIndex = 0;
		this.timer = -1;
	};
	
	//图片路径转换
	Model.prototype.getImageUrl = function(url) {
		return require.toUrl(url);
	};
	//获取轮换图片
	Model.prototype.imgDataCustomRefresh = function(event) {
//        var me = this;
//        var url = this.getParentModel().serverUrl + "/ex_community/a/api/community/getNewsListByCategoryId?callback=?";
//        $.getJSON(
//	        url,
//	        function(data){
//	           ///console.log(data);
//	        	var imageArray = data.data.images.split('|');
//	        	
//	        	for(var i = 1;i<imageArray.length;i++){
//	        	    var content = me.comp("contents1").add({});
//	        		if(i == 1){
//	        		   content.addClass('active');
//	        		 }   
//        			
//        			var image = $('<img alt=""   class="image-wall" height="200px" src="'+me.getParentModel().serverUrl + imageArray[i]+'"></img>');
//        			justep.Bind.addNodes(content.domNode, image);
//	        		
//	        	}
//	        }
//        );
		var imgData = this.comp("imgData");
//		var popOverLoading = this.comp("popOverLoading");
		var self = this;
//		popOverLoading.show();
		$.ajax({
			url : this.getParentModel().serverUrl+"/ex_community/a/api/community/getNewsListByCategoryId",
			async : false,
			cache : false,
			data : {

			},
			dataType : "jsonp",
			jsonp : "callback",
			success : function(data) {
			console.log(data.data);
						imgData.loadData(data.data);
						var i=0;
			        	imgData.each(function(param){
			        	var newContent = self.comp('bannerContents').add({});
			            var div = $("<div xid='div"+i+"'style='width:100%,height:200px'><img src='"+ self.getParentModel().serverUrl + param.row.val("image") +"'  xid='image" + i + "' class='image-wall img1' style='width:100%,height:200px' data-bind='click:topClick' id='" + param.row.val("id") + "'></img></div>");
			            justep.Bind.addNodes(newContent.domNode, div);
			            
			            var ul = self.getElementByXid('ol1');
			            var li = document.createElement("li");
			            li.setAttribute("index", i);
				        ul.appendChild(li);
			            i=i+1;
			        });
			},
			error : function() {
				throw justep.Error.create("加载数据失败");
			}
		});

	};
	
	Model.prototype.topClick = function(event){
		var index = this.comp("carousel1").activeIndex - 1;
		var id=$(this.getElementByXid("div"+index)).children("img").attr("id");
		var url = require.toUrl('./news-detail.w');
		justep.Shell.showPage(url, {
			id : id
		});
	};
	
	Model.prototype.contents1ActiveChange = function(event){
		$(".carousel-indicators li").removeClass('active');
		var to = parseInt(event.to);
		if (!isNaN(to)) {
			var li = $(".carousel-indicators li").eq(to);
			li.addClass('active');
		}
	};
	
	
	//周边商家
	Model.prototype.shopsBtnClick = function(event) {
		//justep.Shell.showPage("$UI/communityOto/home/nearShop.w");
		this.getParentModel().goTo("$UI/communityOto/home/nearShop.w");
	};

	
	//社区公告
	Model.prototype.notifyBtnClick = function(event){	 
		//justep.Shell.showPage("$UI/communityOto/community/channel/notify.w?type=community");	
		this.getParentModel().goTo("$UI/communityOto/community/channel/notify.w?type=notify");
	};
		
	
    //报修
	Model.prototype.repaireBtnClick = function(event){
	    //justep.Shell.showPage("$UI/communityOto/home/repaire.w");
	    this.getParentModel().goTo("$UI/communityOto/home/repaire.w");
	};


	Model.prototype.onTouchStart = function(event){
		$(event.currentTarget).next().addClass("touch");
	};
	
	Model.prototype.onTouchEnd = function(event){
		$(event.currentTarget).next().removeClass("touch");
	};


    //物业通知
	Model.prototype.estateBtnClick = function(event){
	   
		//justep.Shell.showPage({url : "$UI/communityOto/community/channel/notify.w?type=area"});
		this.getParentModel().goTo("$UI/communityOto/community/channel/notify.w?type=area");

	};

    //便民电话
	Model.prototype.phoneClick = function(event){
	   
		justep.Shell.showPage("$UI/communityOto/home/phone.w");
	};
	
	

    //商家商城
	Model.prototype.MerchantMallBtnClick = function(event){
	   
		//justep.Shell.showPage("$UI/communityOto/home/MerchantMall.w");
		this.getParentModel().goTo("$UI/communityOto/home/MerchantMall.w");
	};

    //社区商城
	Model.prototype.communityMallBtnClick = function(event){
		//justep.Shell.showPage("$UI/communityOto/home/communityShop.w");
		this.getParentModel().goTo("$UI/communityOto/home/activity-webview.w");
		
	};
	
    //广告添加
	Model.prototype.addAdvisterClick = function(event){
	    if(this.getParentModel().getParentModel().isLogin()){
	    	justep.Shell.showPage("$UI/communityOto/home/communityAd.w");
	    }else{
	    	justep.Shell.showPage("$UI/communityOto/mine/login.w");
	    }
		
	};
	
	//我要发布
	Model.prototype.goToPublish = function(event){
		this.getParentModel().goTo("$UI/communityOto/home/goToPublish.w");
	}; 
    
   
    //获取资讯信息
	Model.prototype.infoDataCustomRefresh = function(event){
		var  url = this.getParentModel().serverUrl + "/ex_community/a/api/community/getHomeInformation?callback=?";
		$.getJSON(url,function(data){
			event.source.loadData(data.data);
		
		});

	};
	
	//返回咨询缩略图
	Model.prototype.getInfoImageUrl = function(path){
		if(path){
			return this.getParentModel().serverUrl + ""+path;
		}else{
			return require.toUrl("$UI/communityOto/img/share_icon.png");
		}
	
	};
    
	Model.prototype.hotDataCustomRefresh = function(event){
		var url = this.getParentModel().serverUrl + "/ex_community/a/api/community/getNotifyNew?callback=?";
		
		$.getJSON(url,function(data){
		
			if(data&&data.data){
			    //为了循环滚动，需要重复列表
			    var tempArray = data.data.concat(data.data);
				event.source.loadData(tempArray);
				    var width = $('.hotInfoList').outerWidth(true);
				    var originLeft =  $('.hotInfoList').offset().left;//初始left
				    $('.hotInfoList').animate({left:"-="+width/2+"px"},25000,"linear",function(){
        
				        	setInterval(function(){
				        	      
								if($('.hotInfoList').offset() && $('.hotInfoList').offset().left == originLeft-width/2){
								    
								    //滚动完一半后，从新移到初始位置，再滚动
									$('.hotInfoList').offset({left:originLeft});
									$('.hotInfoList').animate({left:"-="+width/2+"px"},25000,"linear");       
									
								}
					        
					        },500);
				        		    
				    });//滚动的距离为长度的一半
			        
			}
		});


	};
  
	Model.prototype.list3Click = function(event){
		var  row = this.comp("hotData").getCurrentRow();
		var option = {
				type:'notify',
				title:row.val("title"),
				content:row.val("content")
		};
		
		justep.Shell.showPage("$UI/communityOto/home/hotContent.w?",option);

	};
  
    
	Model.prototype.list1Click = function(event){
	    
	    var row = this.comp("infoData").getCurrentRow();

		justep.Shell.showPage("$UI/communityOto/home/hotContent.w?hotId="+row.val('id'));

	};
    
 
    
//	Model.prototype.list3AfterRender = function(event){
//	    
//	    if(++this.renderIndex==this.comp("hotData").getCount()){
//	    	var homeSrc = this.getParentModel().comp('homeContainer').curSrc;
//	    	
//			  if(homeSrc == "$model/UI2/communityOto/home/home.w"  ||  homeSrc == "$UI/communityOto/home/home.w"){
//			    	 
//			    	var width = $('.hotInfoList').outerWidth(true);
//				    $('.hotInfoList').animate({left:"-="+width/2+"px"},15000,"linear",function(){
//				       
//				        	if(this.getParentModel().timer == -1){
//					            var originLeft =  $('.hotInfoList').offset().left;//初始left
//						        
//					        	this.getParentModel().timer = setInterval(function(){
//					        	      
//									if($('.hotInfoList').offset().left == originLeft-width/2){
//									    
//									    //滚动完一半后，从新移到初始位置，再滚动
//										$('.hotInfoList').offset({left:originLeft});
//										$('.hotInfoList').animate({left:"-="+width/2+"px"},15000,"linear");       
//										
//									}
//						        
//						        },500);
//					        }
//				        
//				       
//				    
//				    });//滚动的距离为长度的一半
//			        
//			    
//			   }
//
//	    }
//		
//
//	};
//    

  
	
    
   
    
	
    
	
    
   
    
	Model.prototype.secondHandClick = function(event){
		this.getParentModel().goTo("$UI/communityOto/home/secondHand.w");
	};    

  
	
    
   
    
	
    
	
    
   
    
	return Model;
});